package com.itchen.headline.service.impl;

import com.itchen.headline.dao.NewsHeadLineDao;
import com.itchen.headline.dao.impl.NewsHeadlineDaoImpl;
import com.itchen.headline.pojo.NewsHeadline;
import com.itchen.headline.pojo.vo.HeadlineDetailVo;
import com.itchen.headline.pojo.vo.HeadlinePageVo;
import com.itchen.headline.pojo.vo.HeadlineQueryVo;
import com.itchen.headline.service.NewsHeadlineService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewsHeadlineServiceImpl implements NewsHeadlineService {
    private NewsHeadLineDao newsHeadLineDao = new NewsHeadlineDaoImpl();

    /**
     * 注意：查询所有头条的json串格式
     * 进行业务处理：将数据 规整化
     * @param headlineQueryVo
     * @return
     */
    @Override
    public Map findPage(HeadlineQueryVo headlineQueryVo) {
        int pageNum = headlineQueryVo.getPageNum();
        int pageSize = headlineQueryVo.getPageSize();
        List<HeadlinePageVo> pageData = newsHeadLineDao.findPageList(headlineQueryVo);
        int totalSize = newsHeadLineDao.findPageCount(headlineQueryVo);
        int totalPage = (totalSize%pageSize == 0) ? (totalSize/pageSize) : (totalSize/pageSize + 1);
        Map pageInfo = new HashMap();
        pageInfo.put("pageNum",pageNum);
        pageInfo.put("pageSize",pageSize);
        pageInfo.put("totalSize",totalSize);
        pageInfo.put("totalPage",totalPage);
        pageInfo.put("pageData",pageData);
        return pageInfo;
    }

    @Override
    public HeadlineDetailVo findHeadlineDetail(int hid) {
        // 修改浏览量 +1
        newsHeadLineDao.incrPageViews(hid);
        // 查询详情
        return newsHeadLineDao.findHeadlineDetail(hid);
    }

    @Override
    public int addNewsHeadline(NewsHeadline newsHeadline) {
        return newsHeadLineDao.addNewsHeadline(newsHeadline);
    }

    @Override
    public NewsHeadline findHeadlineByHid(int hid) {
        return newsHeadLineDao.findHeadlineByHid(hid);
    }

    @Override
    public int update(NewsHeadline newsHeadline) {
        return newsHeadLineDao.update(newsHeadline);
    }

    @Override
    public int removeByHid(int hid) {
        return newsHeadLineDao.removeByHid(hid);
    }
}
